package descentFly.sound {
	import UIFree.ObjectEvent;
	
	import away3d.audio.Sound3D;
	import away3d.audio.drivers.SimplePanVolumeDriver;
	import away3d.containers.ObjectContainer3D;
	import away3d.containers.Scene3D;
	import away3d.containers.View3D;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.geom.Matrix3D;

	public class df_soundManager extends EventDispatcher {
		public static const SOUNDPLAY: String = 'SOUNDPLAY';
		public static const SOUNDSTOP: String = 'SOUNDSTOP';
		
		private static var _instance: df_soundManager;
		private var _sounds	: Array;
		private var _view	: View3D;
		
		public static function instance(): df_soundManager {
			return _instance;
		}
		
		public static function playSound(soundObj: Object, refTransf: Object=null): void {
			_instance.play(soundObj, refTransf);
		}
		
		public static function stopSound(soundObj: Object): void {
			_instance.stop(soundObj);
		}
		
		public function df_soundManager(a_view: View3D) {
			super(null);
			_instance 	= this;
			_view 		= a_view;
			_sounds 	= new Array();
			addEventListener(SOUNDPLAY, doPlay);
			addEventListener(SOUNDSTOP, doStop);
		}
		
		public function getSound(fileName: String): df_sound {
			var _result: df_sound = null;
			_sounds.forEach(function(item: df_sound, index: int, arr: Array): void {
				if (item.fileName == fileName)
					_result = item;
			});
			return _result;
		}
		
		public function stopAll(): void {
			_sounds.forEach(function(item: df_sound, index: int, arr: Array): void {
				item.Stop();
			});
		}
		
		public function play(sObj: Object, ref: Object=null): void {
			var sound: df_sound = getSound(sObj.url);
			function getTransform(ref: Object): Matrix3D {
				if (ref.hasOwnProperty('transform')) return ref.transform;
				else if (ref is Matrix3D) return ref as Matrix3D;
				return new Matrix3D(); 
			}
			
			function soundPlay(): void {
				
				if (ref) {
					var s3d: df_sound3D = new df_sound3D(sound, _view.camera, new SimplePanVolumeDriver, sObj.volume);
					s3d.transform = getTransform(ref);
					s3d.play();
				} else sound.Play(0, sObj.loops, sObj.volume);
			}
			
			if (!sound) {
				function doComplete(e: Event): void {
					sound.removeEventListener(Event.COMPLETE, doComplete);
					soundPlay();
				}
				
				sound = new df_sound(sObj.url); 
				sound.addEventListener(Event.COMPLETE, doComplete);
				_sounds.push(sound);
			} else soundPlay();
		}
		
		public function stop(sObj: Object): void {
			function v_doComplete(e: Event): void {
				sound.removeEventListener(Event.COMPLETE, v_doComplete);
				sound.Stop();
			}
			
			var sound: df_sound = getSound(sObj.url);
			if (sound) {
				if (sound.loaded) sound.Stop();
				else sound.addEventListener(Event.COMPLETE, v_doComplete);
			}
		}
		
		protected function doPlay(e: ObjectEvent): void {
			play(e.object);
		}
		
		protected function doStop(e: ObjectEvent): void {
			stop(e.object);

		}
	}
}